package main

import (
	"fmt"
	"log"
	"net/http"

	"golang.org/x/net/websocket"
	//"encoding/hex"
)

func Echo(ws *websocket.Conn) {
	var err error
	for {
		var reply string

		if err = websocket.Message.Receive(ws, &reply); err != nil {
			log.Println("can't receive")
			break
		}
		log.Println("receive from client: " + reply)
		msg := "Received: " + reply

		log.Println("send to  client: " + msg)

		if err = websocket.Message.Send(ws, msg); err != nil {
			log.Println("can't send")
			break
		}

	}
}

func main() {
	fmt.Println("Web start")
	//http.HandleFunc("/webSocketServer", Echo)
	http.Handle("/webSocketServer", websocket.Handler(Echo))
	err := http.ListenAndServe(":1234", nil)
	if err != nil {
		log.Fatal("langxw Listen and serve", err)
	} else {
		fmt.Println("Web ready")
	}
}
